package com.lu.servlet;

import com.lu.pojo.Goods;
import com.lu.pojo.Page;
import com.lu.service.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "GoodsServlet", urlPatterns = {"/Goods"})
public class GoodsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        PrintWriter out = resp.getWriter();
        // 获得opr参数
        String opr = req.getParameter("opr");
        if ("getAll".equals(opr)) {
            int total = new GoodsServiceImpl().getCountGoods();
            int count = 10;
            Page page = new Page(total, count);
            if (req.getParameter("currentPage") != null) {
                int c = Integer.parseInt(req.getParameter("currentPage"));
                page.setCurrentPage(c);
            }
            List<Goods> allGoodsList = new GoodsServiceImpl().getSomeGoodsByPage(page);
            req.getSession().setAttribute("Page", page);
            req.getSession().setAttribute("allGoodsList", allGoodsList);
            req.getRequestDispatcher("goodsList.jsp").forward(req, resp);
        } else if ("getOne".equals(opr)) {
            int gid = Integer.parseInt(req.getParameter("gid"));
            Goods goods = new GoodsServiceImpl().getOneGoodsById(gid);
            req.getSession().setAttribute("good", goods);
            req.getRequestDispatcher("OneGoods.jsp").forward(req, resp);
        } else if ("getSome".equals(opr)) {
            int uid = Integer.parseInt(req.getParameter("uid"));
            List<Goods> someGoodsList = new GoodsServiceImpl().getSomeGoodsByUid(uid);
            req.getSession().setAttribute("someGoodsList", someGoodsList);
            req.getRequestDispatcher("someGoodsList.jsp").forward(req, resp);
        } else if ("add".equals(opr)) {
            String uname = req.getParameter("uname");
            int uid = Integer.parseInt(req.getParameter("uid"));
            String gname = req.getParameter("gname");
            int tid = Integer.parseInt(req.getParameter("tid"));
            Double price = Double.parseDouble(req.getParameter("price"));
            String description = req.getParameter("description");

            Goods goods = new Goods(tid, uid, gname, price, description, uname);
            int i = new GoodsServiceImpl().addOneGoods(goods);
            if (i > 0) {
                out.print("<script>alert('添加成功!');location.href = 'admin_1.jsp';</script>");
            } else {
                out.print("<script>alert('添加失败!');history.back();</script>");
            }
        } else if ("edit".equals(opr)) {

        } else if ("editSave".equals(opr)) {

        } else if ("del".equals(opr)) {
            int total = new GoodsServiceImpl().getCountGoods();
            int count = 10;
            Page delPage = new Page(total, count);
            if (req.getParameter("currentPage") != null) {
                int c = Integer.parseInt(req.getParameter("currentPage"));
                delPage.setCurrentPage(c);
            }
            List<Goods> delGoodsList = new GoodsServiceImpl().getSomeGoodsByPage(delPage);
            req.getSession().setAttribute("delPage", delPage);
            req.getSession().setAttribute("delGoodsList", delGoodsList);
            req.getRequestDispatcher("deleteGoods.jsp").forward(req, resp);
        } else if ("delSave".equals(opr)) {
            int gid = Integer.parseInt(req.getParameter("gid"));
            int i = new GoodsServiceImpl().delOneGoods(gid);
            if(i>0){
                out.print("<script>alert('删除成功!');location.href = 'Goods?opr=getAll';</script>");
            }else {
                out.print("<script>alert('删除失败!');history.back();</script>");
            }
        }

        out.flush();
        out.close();

    }
}
